/**
 * 
 */
package anyware.desktop.model;

import java.io.File;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Transient;
import javax.persistence.Version;

import org.apache.commons.lang.Validate;

/**
 * @author <a href="mailto:keke@codehaus.org">keke</a>
 * 
 */
@Entity
public class FileItem implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 8499068657890269175L;
    @Transient
    private File              file;
    @Id
    @GeneratedValue
    private Long              id;
    private String            name;
    @Column(unique = true)
    private String            pathUrl;
    @OneToOne(optional = false, cascade = CascadeType.ALL)
    private FileItemInfo    info;
    @Version
    @Column(name = "OPTLOCK")
    private int               version;

    public FileItem() {
        super();
    }

    public FileItem(final File file) {
        super();
        Validate.notNull(file);
        this.file = file;
        info = new FileItemInfo();
        info.setLastModified(new Date(file.lastModified()));
        info.setSize(file.length());
        name = file.getName();
        pathUrl = file.toURI().toString();
    }

    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final FileItem other = (FileItem) obj;
        if (pathUrl == null) {
            if (other.pathUrl != null) {
                return false;
            }
        } else if (!pathUrl.equals(other.pathUrl)) {
            return false;
        }
        return true;
    }

    @Transient
    public File getFile() {
        if (file == null) {
            try {
                file = new File(new URL(pathUrl).toURI());
            } catch (final MalformedURLException e) {
                throw new RuntimeException(e);
            } catch (final URISyntaxException e) {
                throw new RuntimeException(e);
            }
        }
        return file;
    }

    public Long getId() {
        return id;
    }

    public FileItemInfo getInfo() {
        return info;
    }

    public String getName() {
        return name;
    }

    public String getPathUrl() {
        return pathUrl;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + (pathUrl == null ? 0 : pathUrl.hashCode());
        return result;
    }

    public void setInfo(final FileItemInfo status) {
        info = status;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public void setPathUrl(final String pathUrl) {
        this.pathUrl = pathUrl;
    }

}
